<template>
<div class="page flex">
  <!--菜单栏-->
  <div class="menu-side">
    <!--<file-tree :data="data" :level="0"/>-->
    <el-tree :data="directions.data" :props="defaultProps" highlight-current accordion @node-click="handleNodeClick"></el-tree>
  </div>
  <div class="flex-1 ml-10 mr-10">
    <!--导航栏-->
    <div class="content-head">
      <el-form inline class="flex">
        <el-form-item>
          <el-checkbox-group v-model="query.docTypes">
            <el-checkbox label="doc" name="docTypes">文档</el-checkbox>
            <el-checkbox label="img" name="docTypes">图片</el-checkbox>
            <el-checkbox label="mp4" name="docTypes">视频</el-checkbox>
            <el-checkbox label="mp3" name="docTypes">音频</el-checkbox>
          </el-checkbox-group>
        </el-form-item>

        <el-form-item>
          <el-input placeholder="请输关键字进行过滤" v-model="query.keywords" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="deepSearch">深度搜索</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="deepSearch">批量下载</el-button>
        </el-form-item>
        <div class="flex-1"></div>
        <el-form-item>
          <el-select v-model="lineNumber" style="width:110px;" placeholder="显示数量">
            <el-option label="6张" value="6"/>
            <el-option label="10张" value="10"/>
            <el-option label="15张" value="15"/>
            <el-option label="20张" value="20"/>
          </el-select>
          <el-select v-model="sortType" style="width:110px;">
            <el-option label="时间最新" value="time-asc"/>
            <el-option label="时间最久" value="time-desc"/>
          </el-select>
        </el-form-item>
        <el-form-item>
          <i v-if="showType === 'detail'" class="el-icon-s-grid show-type" @click="showType = 'list'" />
          <i v-if="showType === 'list'" class="el-icon-s-unfold show-type" @click="showType = 'detail' "/>
        </el-form-item>
      </el-form>
    </div>
    <!--内容区-->
    <div class="content-container flex">
      <div id="content-container" class="flex-1">
        <div v-for="(file,index) of showFiles" :key="index" class="file-item" :style="{width: imgWidth}">
          <div class="file-container" @click="selectFile(index)">
            <div class="select-btn" :class="{checked: file.checked}" :style="{width: checkBtnWidth, height: checkBtnWidth}"></div>
            <!--显示图片-->
            <div v-if="file.docType === 'img'" class="file" :style="{backgroundImage: 'url(' + file.thumb + ')',height: imgWidth}" @dblclick="download(file)"></div>
            <el-image v-if="file.docType === 'txt'" class="file" src="/icons/txt.png" /><!--文档-->
            <audio v-if="file.docType === 'mp3'" class="file" :src="file.url" controls /><!--mp3-->
            <el-image v-if="file.docType === 'mp3'" class="file" src="/icons/mp3.png" /><!--mp3-->
            <video v-if="file.docType === 'mp4'" class="file" :src="file.url" controls /><!--mp4-->
          </div>
          <div class="file_title flex flex-center">
            <p class="flex-1">{{file.virtual_path}}</p>
            <p>
              <i @click="download(file)" class="el-icon-download" style="font-size:20px;font-weight: bold;color:#067aa0;margin-top:5px;cursor: pointer;"></i>
            </p>
          </div>
        </div>
      </div>
      <div class="download-container">
        <a v-for="(item,index) of 10" :key="index" href="">
          <img src="http://localhost:8000/upload/298dc2cee369cdbe5a250328b41b40f9.png" style="display:block;width:100%;margin-bottom:10px;">
        </a>
      </div>
    </div>
  </div>
</div>
</template>

<script>
// import FileTree from '@/components/FileTree'
import ApiFile from '@/api/ApiFile'
import { docType, basename } from '@/utils/tools'

export default {
  name: 'Tree',
  components: { },
  data() {
    return {
      query:{
        docTypes: [],
        keywords: '',
      },
      showType:'detail', //两种状态： list, detail
      sortType: '',
      lineNumber: 6,
      directions: {
        data: [],
        current: '',
        currentNode: null,
      },
      files: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      }
    };
  },
  computed: {
    showFiles() {
      let files = []
      let typeLen = this.query.docTypes.length
      for(const file of this.files) {
        const fileName = basename(file.name)
        console.log(file)
        if ((typeLen === 0 || this.query.docTypes.indexOf(file.docType) >= 0) && (this.query.keywords === '' || fileName.indexOf(this.query.keywords) >= 0)) {
          files.push(file)
        }
      }
      return files
    },
    imgWidth() {
      const total = document.getElementById('content-container').offsetWidth - 15
      const width = parseInt(total / this.lineNumber) - 13
      return width + 'px'
    },
    checkBtnWidth() {
      return parseInt(90 / Math.sqrt(this.lineNumber)) + 'px'
    }
  },
  created() {
    this.getDirections()
  },
  methods: {
    handleNodeClick(data, node) {
      console.log(data);
      this.directions.current = data.id
      this.directions.currentNode = node
      // 寻找孙子辈
      if (data.children !== null) {
        for(let child of data.children) {
          console.log(child)
          if (child.children === null) {
            ApiFile.scanFile(child.id, 2, 2).then(res => {
              child.children = res.data
            })
          }
        }
      }

      // 获取文件
      ApiFile.scanFile(this.directions.current, 1, 1).then(res => {
        const files = res.data
        for(let file of files) {
          file.docType = docType(file.virtual_path)
          file.checked = false
        }
        this.files = files
        console.log(this.files)
      })
    },
    getDirections() {
      ApiFile.scanFile(0, 2, 2).then(res => {
        console.log(res)
        if (this.directions.currentNode) {
          if (res.data.length > 0) {
            this.directions.currentNode.data.children = res.data
          }
        } else {
          this.directions.data = res.data;
        }
      }).catch(res => {
        console.log(res)
      })
    },
    deepSearch() {

    },
    download(file){
      console.log(file)
      const a = document.createElement("a");
      a.download = file.url
      a.href = file.url
      a.target = "_blank"
      document.body.appendChild(a)
      a.click()
      a.remove()
    },
    selectFile(index) {
      let file = this.files[index]
      file.checked = !file.checked
    }
  }
}
</script>

<style scoped lang="scss">
.page{
  min-height: calc(100vh - 70px - 10px);
  margin-top:10px;
}
.flex{
  align-items: stretch;
}

/*目录树*/
.menu-side{
  padding:10px;
  background-color: #444;
  .el-tree{
    min-width:150px;
  }
  /deep/ .el-tree--highlight-current .el-tree-node.is-current{
    >.el-tree-node__content {
      background-color: #409EFF;
      color:#fff;
    }
  }
  /deep/ .el-tree--highlight-current .el-tree-node.is-current .el-tree-node__expand-icon,
  /deep/ .el-tree-node__content,
  /deep/ .el-tree-node__expand-icon {
    color:#000;
  }

  /deep/ .el-tree-node__content{
    padding-right:15px;
  }
}

.content-head{
  height:50px;
  border-bottom: 1px solid #ccc;

  .el-checkbox{
    margin-right:10px;
  }
  /deep/ .el-checkbox__label{
    padding-left:5px;
  }

  .show-type{
    font-size:20px;
  }
}
.download-container{
  width: 90px;
  background:#000;
  padding: 5px;
}
.content-container{
  background-color: #ddd;
  padding:5px;
  overflow-y: scroll;
  height:calc(100vh - #{$navHeight} - 20px - 50px);
  //text-align: center;
  .file-item{
    display: inline-block;
    margin:5px;
    position: relative;
    background-color: #545c64;
    overflow: hidden;

    &:hover .select-btn{
      display: block;
    }
    .select-btn.checked{
      background-image: url("/icons/yes.png");
      background-position: center;
      background-repeat: no-repeat;
      background-size: 70%;
      display: block;
    }
    .select-btn{
      width: 40px;
      height: 40px;
      right: 0;
      top: 0;
      position: absolute;
      z-index: 10;
      background-color: rgb(43, 238, 10);
      border: 3px solid #fff;
      display: none;
    }

    .file{
      display: block;
      width: 100%;
      background-size: contain;
      background-repeat: no-repeat;
      background-position: center;
    }
    audio.file,
    video.file{
      position: absolute;
      width:100%;
      top:60px;
      z-index: 100;
      background-color: #000;
      padding:20px 0;
    }
    .file_title{
      height: 50px;
      padding: 5px;
      background:#eee;
      border: 5px solid #545c64;
      color: #000000;
      font-size:12px;
      word-break: break-all;
      margin:0;
      box-sizing: border-box;
      overflow: hidden;
      p{
        padding: 0;
        margin:0;
      }
    }
  }
}
</style>
